<template>
	<div class="system-user-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="deviceDialogFormRef" :rules="rules" :model="ruleForm" size="default" label-width="120px">
				<el-row>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备编号" prop="code">
							<el-input v-model="ruleForm.code" placeholder="请输入编号" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="设备型号" prop="equipmentmode">
							<el-select v-model="ruleForm.equipmentmode">
								<el-option v-for="item in modeList" :key="item.id" :label="item.name" :value="item.name"></el-option>
							</el-select>
							<!-- <el-input v-model="ruleForm.batchcode" placeholder="请选择型号" clearable></el-input> -->
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts">
import { reactive, ref, nextTick } from 'vue';
import { DeviceVo, useDevice } from './device';
import { ElMessage } from 'element-plus';
import { useUserInfo } from '/@/stores/userInfo';
import { get } from 'lodash-es';
// import { storeToRefs } from 'pinia';
// const userStore = useUserInfo()
// const { userInfo } = storeToRefs(useStore)
const { userInfo } = useUserInfo();
// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);
const modeList = ref([]);
// 定义变量内容
const deviceDialogFormRef = ref();
const ruleForm = reactive<DeviceVo>({
	id: '',
	code: '',
	dealerId: '',
	status: '',
	equipmentmode: '',
	companyId: '',
});
const state = reactive({
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const rules = ref({
	code: [{ required: true, type: 'string', message: '编号不能为空', trigger: 'blur' }],
	equipmentmode: [{ required: true, type: 'string', message: '型号不能为空', trigger: 'blur' }],
});

// 打开弹窗
const openDialog = () => {
	state.dialog.title = '新增设备';
	state.dialog.submitTxt = '新 增';
	// 清空表单，此项需加表单验证才能使用
	nextTick(() => {
		deviceDialogFormRef.value.resetFields();
	});
	state.dialog.isShowDialog = true;
	useDevice()
		.getModeList(userInfo!.companyId!)
		.then((res) => {
			modeList.value = res;
		});
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	if (!deviceDialogFormRef.value) return;
	if (userInfo?.companyId) {
		ruleForm.companyId = userInfo?.companyId;
		deviceDialogFormRef.value.validate((valid: boolean) => {
			if (valid) {
				useDevice()
					.saveDevice(ruleForm)
					.then(() => {
						closeDialog();
						emit('refresh');
						ElMessage.success('新增成功！');
					})
					.catch((err) => {
						ElMessage.error(err.message);
					});
			}
		});
	} else {
		ElMessage.error('获取用户信息失败！');
	}
	// closeDialog();
	// emit('refresh');
	// if (state.dialog.type === 'add') { }
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
